package 课设;

import javax.swing.*;

import java.awt.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;


public class user_show extends JFrame{

    JList<Blog> jList;
    DefaultListModel<Blog> listModel;
    JTable table;
    private MyTableModel tablemodel;
    public user_show(int userId) throws SQLException {
        super("用户信息");
        this.setSize(700,300);
        this.setLocationRelativeTo(null);

        JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
        this.getContentPane().add(splitPane);
        userJFrame a = new userJFrame(userId);
        splitPane.add(a);

        splitPane.setDividerLocation(110);
        splitPane.setOneTouchExpandable(true);

        tablemodel = getModel(userId);
        table = new JTable(tablemodel);
        table.setPreferredScrollableViewportSize(new Dimension(500, 300));

        JPanel right = new JPanel(new BorderLayout());
        splitPane.add(right);
        JScrollPane t = new JScrollPane(table);
        right.add(t);

        JPanel cmd = new JPanel();
        right.add(cmd,"South");

        JButton b1 = new JButton("添加");
        cmd.add(b1);
        b1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                db dbcon = new db();
                blogAdd padd = new blogAdd(userId);
                padd.setVisible(true);
                dispose();
            }
        });

        JButton b3 = new JButton("删除");
        cmd.add(b3);
        b3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                db dbcon = new db();
                try {
                    if(table.getSelectedRows().length > 0) {
                        //获得JTable中选中行的序列
                        int[] selRowIndexs = table.getSelectedRows();
                        PreparedStatement presta = dbcon.PreparedStatement("delete from blog where blogId = ?");

                        for(int i = 0;i<selRowIndexs.length;i++) {
                            presta.setInt(1, (int)table.getValueAt(selRowIndexs[i], 0)); //presta.setString(1, table.getValueAt(______,0).toString());
                            presta.executeUpdate();
                        }
                        //重新加载数据到JTable
                        tablemodel = getModel(userId);
                        table.setModel(tablemodel);
                    }
                }catch(SQLException sqle) {
                    System.out.println(sqle.toString());
                }
            }
        });

        JButton b4 = new JButton("查看");
        cmd.add(b4);
        b4.addActionListener(new ActionListener() {//查看选中文件
            public void actionPerformed(ActionEvent e) {
                db dbcon;
                try{
                    dbcon = new db();
                    if(table.getSelectedRows().length > 0) {
                        //获得JTable中选中行的序列
                        int[] selRowIndexs = table.getSelectedRows();
                        if(selRowIndexs != null) {
                            int blogId = (int) table.getValueAt(selRowIndexs[0], 0);
                            //System.out.println("-------");
                            dispose();
                            bolg_select_update a = new bolg_select_update(blogId,userId);
                            a.setVisible(true);
                        }
                    }else
                        JOptionPane.showMessageDialog(null,"未选中");
                }catch (Exception ex){
                    System.out.println(ex.getMessage());
                }finally {
                    tablemodel = getModel(userId);
                    table.setModel(tablemodel);
                }
            }
        });

}
    private MyTableModel getModel(int userId) {
        MyTableModel tableModel = new MyTableModel();
        db dbcon;
        try {
            dbcon = new db();
            ResultSet rs = dbcon.executeQuery("select * from blog where userId=" + userId);

            ResultSetMetaData rsmd = rs.getMetaData();//获得查询结果中的列名，填充表格模型列
            int Colnum = rsmd.getColumnCount();
            int i;
            for (i = 1; i <= Colnum; i++)
                tableModel.addColumn(rsmd.getColumnName(i));
            ArrayList<Blog> v = new ArrayList<>();//获取查询结果中的元组，填充表格模型行
            while (rs.next()) {
                Blog blog = new Blog();
                blog.setBlogId(rs.getInt("blogId"));
                blog.setTitle(rs.getString("title"));
                blog.setContent(rs.getString("summary"));
                blog.setPostTime(rs.getTimestamp("postTime"));
                blog.setUserId(rs.getInt("userId"));
                v.add(blog);
            }
            rs.close();
            for (i = 0; i < v.size(); i++) {
                tableModel.addRow(new Object[]{v.get(i).getBlogId(), v.get(i).getTitle(), v.get(i).getContent(),
                        v.get(i).getPostTime(), v.get(i).getUserId()});
            }
        } catch (SQLException sqle) {
            System.out.println(sqle);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return tableModel;
    }
    public static void main(String[] args) throws SQLException {
//        user_show a = new user_show(1);
//        a.setVisible(true);
        System.out.println(Long.MAX_VALUE);
    }
}
